class LoginController < ApplicationController
  layout "login"

  # Prepares for default view of login
  def index
  end

  # Performs the login action. Invoked from login form.
  def login
    user = params[:user]
    user[:password] = User.encrypt(user[:password])
    db_user = User.find(:first, :conditions => ["login = :login AND password = :password", user])

    if db_user 
      clear_flash_info
      set_session_user(db_user)
      redirect_to get_requested_uri || ({:controller => "home"})
      clear_requested_uri
    else
      set_flash_info(FlashInfo.new("Invalid login or password!", FlashInfo::ERROR))
      redirect_to :action => :index
    end
  end

  # Performs the logout action.
  def logout
    clear_session_user
    redirect_to :controller => "home"
  end
end